const app = getApp()
const Api = require("../../utils/api.js")
const ApiRequest = require("../../utils/request.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    motto: '感谢您的反馈！',
    questionnaire: null,
    // questionList: [{
    //     "question": "诊断是否清楚？",
    //     "answerList": ["是", "否"]
    //   },
    //   {
    //     "question": "是否了解疾病性质？",
    //     "answerList": ["良性", "恶性", "不清楚"]
    //   },
    // ],
    selectList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var self = this
    // console.log("options:", options)
    let state = options.state
    let type = options.type
    let role = options.role
    let flowStepId = options.flowStepId
    self.setData({
      flowStepId: flowStepId,
      state: state
    })
    self.chooseType(state, type, role)
  },
  /**
   * 选择问卷类型
   */
  chooseType: function(state, type, role) {
    var self = this
    if (role == 'doctor' || state == 2) { // 查看已填写问卷
      self.showDoneSurvey()
    } else if (state == 1) { // 新建新问卷
      self.newSurvey(type)
    }
  },
  /**
   * 选项初始化
   */
  selectListInit: function(object) {
    var self = this
    let tempSelect = [];
    if (object == 'init') {
      let questionList = self.data.questionnaire.questionList
      let child = {
        "questionId": "-1",
        "answer": "-1"
      }
      for (let i = 0, len = questionList.length; i < len; i++) {
        tempSelect[i] = child
      }
    } else {
      for (let i = 0, len = object.length; i < len; i++) {
        let child = {
          "questionId": object[i].id,
          "answer": object[i].choice
        }
        tempSelect[i] = child
      }
    }
    self.setData({
      selectList: tempSelect
    })
  },
  /**
   * 新建问卷
   */
  newSurvey: function(type) {
    var self = this
    var url = Api.new_questionnaire + '/' + type
    var params = {};
    var method = 'GET';
    var header = "application/json";
    var success = function(res) {
      let data = res.data
      // console.log("data: ", data)
      self.setData({
        questionnaire: data
      })
      // 选项初始化
      self.selectListInit('init')
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },
  /**
   * 查看已完成问卷
   */
  showDoneSurvey: function() {
    var self = this
    var url = Api.new_questionnaire
    var params = {
      flowStepId: self.data.flowStepId
    };
    var method = 'GET';
    var header = "application/json";
    var success = function(res) {
      let data = res.data
      // console.log("查看已完成问卷: ", res)
      self.selectListInit(data.questionList)
      self.setData({
        questionnaire: data,
      })
    }
    ApiRequest.request(url, params, method, header, success, self.fail);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  onChange(e) {
    let selectIdx = e.detail
    let questionIdx = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let temp = this.data.selectList

    let tempChild = {
      "questionId": id,
      "answer": selectIdx.value
    }
    temp[questionIdx] = tempChild
    // console.log(temp)
    this.setData({
      selectList: temp
    })
  },
  checkSelct: function() {
    let selectList = this.data.selectList
    for (let i = 0, len = selectList.length; i < len; i++) {
      if (selectList[i].questionId === "-1" || selectList[i].answer == "-1") {
        wx.showModal({
          title: '提交失败',
          content: '请填完所有选项',
        })
        return
      }
    }
    this.submit()
  },
  /**
   * 用户提交
   */
  submit: function() {
    var self = this
    self.setData({  // 关闭提交按钮
      state: 2
    })
    var url = Api.submit_questionnaire
    var params = {
      flowStepId: self.data.flowStepId,
      selectItemList: self.data.selectList
    };
    var method = 'POST';
    var header = "application/json";
    var success = function(res) {
      let data = res.data
      console.log("data: ", data)
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000
      })
      self.showDoneSurvey()
    }
    ApiRequest.request(url, params, method, header, success, self.fail);

    // setTimeout(function() {
    //   wx.navigateBack({
    //     delta: 1
    //   })
    // })
  }
})